package employeeSalary;

import com.google.gson.Gson;
import salaryEntity.emSalaryEntity;
import tools.BaseResponse;
import tools.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static salaryDao.employeeSalaryDao.addSalaryEmTable;


@WebServlet(urlPatterns = "/ssc/add/")
public class addSalary extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("utf-8");

        // 获取提交的json字符串
        String jsonStu = RequestUtil.getRequestBody(req);

        Gson gson = new Gson();
        // 将JSON字符串转化为实体类
        emSalaryEntity stuBody = gson.fromJson(jsonStu, emSalaryEntity.class);


        int rows=addSalaryEmTable(stuBody);

        resp.setContentType("application/json");
        BaseResponse<Integer> baseResponse = new BaseResponse<Integer>();
        if (rows > 0)
        {
            baseResponse.setCode(200);
            baseResponse.setMsg("添加成功");
        }
        else
        {
            baseResponse.setCode(500);
            baseResponse.setMsg("添加失败");
        }

        PrintWriter out = resp.getWriter();
        out.print(gson.toJson(baseResponse));
        out.flush();
    }
}
